﻿using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using LanShellManager.Models;

namespace LanShellManager.Controllers
{
    public class ShellCommandsController : Controller
    {
        private IShellCommandRepository repository = new ShellCommandRepository();
        //private LanShellManagerContext db = new LanShellManagerContext();

        // GET: /ShelllCommmands/
        public ActionResult Index()
        {
            return View(repository.GetCommands());
        }

        // GET: /ShelllCommmands/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ShellCommandVm commandVm = repository.Find(id.Value);
            if (commandVm == null)
            {
                return HttpNotFound();
            }
            return View(commandVm);
        }

        // GET: /ShelllCommmands/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: /ShelllCommmands/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,Name,Text")] ShellCommandVm commandVm)
        {
            if (ModelState.IsValid)
            {
                repository.Add(commandVm);
                return RedirectToAction("Index");
            }

            return View(commandVm);
        }

        // GET: /ShelllCommmands/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ShellCommandVm commandVm = repository.Find(id.Value);
            if (commandVm == null)
            {
                return HttpNotFound();
            }
            return View(commandVm);
        }

        // POST: /ShelllCommmands/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Name,Text")] ShellCommandVm commandVm)
        {
            if (ModelState.IsValid)
            {
                repository.Update(commandVm);
                return RedirectToAction("Index");
            }
            return View(commandVm);
        }

        // GET: /ShelllCommmands/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ShellCommandVm commandVm = repository.Find(id.Value);
            if (commandVm == null)
            {
                return HttpNotFound();
            }
            return View(commandVm);
        }

        // POST: /Machines/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            ShellCommandVm commandVm = repository.Find(id);
            repository.Delete(id);
            //db.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}
